/**
 * Created by wujiankun on 2016/12/5.
 */
/*
 var product={};


 product.toString=function () {
 var result='';
 for(var s in product){
 if(s == "toString"){
 continue;
 }
 result+=s+":"+product[s]+"\n";
 }
 return result;
 };
 */

function chooseImage(remainCount, callback,index) {
    wx.chooseImage({
        count: remainCount > 0 ? remainCount : 0,// 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
            console.log(res);
            var urls = res.tempFilePaths;
            var progress = 1;
            var resultUrls = [];
            //开始递归上传图片
            uploadImg(urls,resultUrls,progress,callback,index);
        }
    });
}

function uploadImg(urls,resultUrls,progress,callback,index) {
    wx.showToast({
        title: '上传中……' + progress + '/' + urls.length,
        icon: 'loading',
        duration: 10000000
    });
    wx.uploadFile({
        url: 'http://admin.yixuanyoupin.com/wxSmall/uploadImgNew',
        filePath: urls[progress-1],
        name: 'file',
        header: {
            'content-type': 'multipart/form-data',
            'Cookie': 'AUCTION_APP_COOKIE_200039=200039; AUCTION_APP_COOKIE_200007=200007; ' +
            'AUCTION_APP_COOKIE_200037=200037; AUCTION_APP_COOKIE_200041=200041; ' +
            'AUCTION_APP_COOKIE_200040=200040; JSESSIONID=701BAD9272E753071D9E779A5EDDA9F1;' +
            ' AUCTION_APP_COOKIE_USERID=200171'
        },
        formData: {
            'index': index?index:progress-1
        },
        success: function (res) {
            console.log(res.data);
            var result = JSON.parse(res.data).data;
            console.log(result);
            progress++;
            wx.hideToast();
            resultUrls.push({
                bigUrl: result.bigUrl,
                index: result.index,
                smallUrl: result.smallUrl
            });
            if (progress>=urls.length) {//上传结束
                callback(resultUrls);
            }else{
                uploadImg(urls,resultUrls,progress,callback);
            }
        }
    });
}

function onTextChange(event, callback) {
    var eventType = event.currentTarget.dataset.type;
    var currentValue = event.detail.value;
    callback(eventType, currentValue);
    /* if(eventType=="name"){//用户输入拍品标题
     product.name = currentValue;
     }else if(eventType=="desc"){//用户输入拍品描述
     product.desc = currentValue;
     }
     console.log("拍品信息："+product.toString());*/
}
var classifyList = [
    '玉翠珠宝： 和田翡翠，琥珀蜜蜡，玛瑙水晶等', '字画篆刻： 国画，书法，印章，印石等', '文玩杂项： 工艺品，手串，木雕，紫砂等杂项'
];
function showClassify(type, callback) {
    switch (type) {
        case 'classify':
            wx.showActionSheet({
                itemList: classifyList,
                success: function (res) {
                    if (!res.cancel) {
                        callback(res.tapIndex, classifyList[res.tapIndex]);
                    }
                }
            });
            break;
        case 'startTime':
            wx.navigateTo({
                url: 'test?type=startTime'
            })
            break;
        case 'endTime':
            wx.navigateTo({
                url: 'test?type=endTime'
            })
            break;
    }
}
function getClassifyByIndex(index) {
    return classifyList[index].split('：')[0];
}

function getImageListByDraftData(draftData) {
    var imageList = [];
    imageList.push({
        bigUrl: draftData.productPic1,
        index: 0,
        smallUrl: draftData.productPic1Big
    });
    for (var i = 0; i < getAttrCount(draftData.prodExtend); i++) {
        imageList.push({
            bigUrl: draftData.prodExtendBig['productPic' + (i + 2)],
            index: i + 1,
            smallUrl: draftData.prodExtend['productPic' + (i + 2)]
        });
    }
    return imageList;
}
function getAttrCount(obj) {
    if (Object.prototype.hasOwnProperty('__count__')) {
        return obj.__count__;
    } else {
        var count = 0;
        for (var i in obj) {
            if (obj.hasOwnProperty(i)) {
                count++;
            }
        }
        return count;
    }
}


module.exports = {
    chooseImage: chooseImage,
    onTextChange: onTextChange,
    showClassify: showClassify,
    getClassifyByIndex: getClassifyByIndex,
    getImageListByDraftData: getImageListByDraftData
}